// Copyright 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android.apps.moderator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.view.ContextMenu;
import android.view.View;

/**
 * This list entry contains a progress spinner. When it comes into view, more
 * submissions are fetched from the API.
 */
public class MoreSubmissionsListEntry implements ListEntry {

  private final ListSynchronizer listSynchronizer;
  private boolean loading;
  private final int offset;
  private final int seriesId;
  private final int topicId;
  private final Map<Integer, JSONObject> votesBySubmissionId;

  public MoreSubmissionsListEntry(int seriesId, int topicId, int offset,
      ListSynchronizer listSynchronizer, Map<Integer, JSONObject> votesBySubmissionId) {
    this.seriesId = seriesId;
    this.topicId = topicId;
    this.offset = offset;
    this.listSynchronizer = listSynchronizer;
    this.votesBySubmissionId = votesBySubmissionId;
    loading = false;
  }

  public void addContextItems(ContextMenu menu, Activity activity) {
    // No context menu
  }

  public boolean clicked(Activity activity) {
    // No action on clicked
    return false;
  }

  public void customizeView(View view, Activity activity) {
    // When the customizer is called on this view, it means it is on the screen, and therefore more
    // entries for display should be fetched. When they are ready they are added to the list and
    // this 'more' indicator is removed.
    if (!loading) {
      loading = true;
      final TopicActivity baseActivity = (TopicActivity) activity;

      new Thread(new Runnable(){
        public void run() {
          try {
            List<ListEntry> entries = new ArrayList<ListEntry>(listSynchronizer.getEntries());
            entries.remove(entries.size() - 1); // Remove the last entry, i.e. this entry! 
            baseActivity.getSubmissions(seriesId, topicId, offset, entries, votesBySubmissionId,
                listSynchronizer);
            listSynchronizer.setEntries(entries);
          } catch (AccessException e) {
            // An error occurred... log and retry
            e.printStackTrace();
            loading = false;
          } catch (JSONException e) {
            // An error occurred... log and retry
            e.printStackTrace();
            loading = false;
          }
        }}).start();
    }
  }

  public int getLayoutId() {
    return R.layout.more_submissions_entry;
  }
}
